package 富婆梦.第一阶段.Java基础视频学习.线程的练习.练习;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
 * @ProjectName: java-learning
 * @Package: 富婆梦.第一阶段.线程的练习.练习
 * @ClassName: Demo6
 * @Author: hemengjin
 * @Description: 两个线程都去运行同一个run方法，但是一个线程是从1加到100，一个线程从1加到200
 * @Date:2021/11/1 20:35
 * @Version: 1.0
 */
public class Demo6 implements Callable<Integer> {
    int num;

    private Demo6(int a) {
        this.num = a;
    }

    @Override
    public Integer call() throws Exception {
        int count = 0;
        for (int i = 1; i <= num; i++) {
            count = count + i;
        }
        return count;
    }

    public static void main(String[] args) {
        ExecutorService ex = Executors.newFixedThreadPool(2);
        Future<Integer> f1 =ex.submit(new Demo6(100));
        Future<Integer> f2 =ex.submit(new Demo6(200));
        try {
            System.out.println(f1.get());
            System.out.println(f2.get());
        }catch (Exception e){
            e.printStackTrace();
        }

    }
}
